package com.ynet.h2.db;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ynet.excetion.PlatformErrorCode;
import com.ynet.utils.DateUtils;
import com.ynet.utils.MD5;

/**
 * 用户信息表
 * @author liuxin
 * 2018年1月31日
 */
public class UserInfo {
	private final static Logger logger = LoggerFactory.getLogger(UserInfo.class);

	/**
	 * 用户注册
	 */
	public Map<String, String> saveUserInfo(String appid, String user_name, String password, String agreement){
		
		Map<String, String> dataMap = new Message().getMessage();
		password = MD5.getMD5ofStr(password);
		
		Statement stmt = null;
		Connection conn = null;
		try{
			conn = new ConnectionPool().getConnection();
			stmt = conn.createStatement();
			stmt.execute("insert into user_info (appid, user_name, password,agreement,logon_time) values("
					+ "" + "'" + appid + "'," +"'" + user_name + "',"+"'" + password + "',"+"'" + agreement + "'," +"'" + DateUtils.getCurrentDate() + "'" + ")");
		}catch(Exception e){
			dataMap.put(PlatformErrorCode.error5, "主键重复");
			e.printStackTrace();
		}finally{
			try {
				if(stmt != null)stmt.close();
				if(conn != null)conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		return dataMap;
	}
	
	/**
	 * 用户登陆验证
	 */
	public boolean logonByAppidAndPassword(String appid, String password){
		
		Statement stmt = null;
		Connection conn = null;
		try{
			conn = new ConnectionPool().getConnection();
			stmt = conn.createStatement();
			String sql = "select appid from user_info where appid=" + "'" + appid +"' and password="  + "'" + password +"'";
			logger.debug(sql);
			
			ResultSet rs = stmt.executeQuery(sql);
			// 遍历结果集
			while (rs.next()) {
				return true;
			}
			
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			try {
				if(stmt != null)stmt.close();
				if(conn != null)conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return false;
	}

	/**
	 * 查询用户信息
	 */
	public Map<String, String> findByAppid(String appid){

		Map<String, String> dataMap = new Message().getMessage();

		Statement stmt = null;
		Connection conn = null;
		try{
			conn = new ConnectionPool().getConnection();
			stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery("SELECT * FROM user_info WHERE appid=" + "'" + appid +"'");
//			logger.debug("--rs.last()--:" + rs.last());
			// 遍历结果集
			while (rs.next()) {
				dataMap.put("user_name", rs.getString("user_name"));
				dataMap.put("appid", rs.getString("appid"));
				dataMap.put("lan_url", rs.getString("lan_url"));
				dataMap.put("agreement", rs.getString("agreement"));
				dataMap.put("logon_time", rs.getString("logon_time"));
				
				logger.debug(rs.getString("user_name") + "," + rs.getString("appid") + "," + rs.getString("lan_url") + "," + rs.getString("agreement") +"," + rs.getString("logon_time"));
			}
		}catch(Exception e){
			dataMap = new Message().getMessage(PlatformErrorCode.error4, "数据库链接异常");
			e.printStackTrace();
		}finally{
			try {
				if(stmt != null)stmt.close();
				if(conn != null)conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	
		return dataMap;
	}

	/**
	 * 修改用户信息
	 */
	public void changeUserInfo(){
		
	}
	
}
